<template>
  <el-dialog :model-value="dialogData.isShow" :title="dialogData.id?'编辑':'新增'"
             @close="closeDialog" draggable>
    <el-form :model="form" ref="formRef" :rules="rules" label-width="90px">
      <el-row :gutter="10">
         <el-col :span="12">
           <el-form-item label="memberId" prop="memberId">
             <el-input v-model="form.memberId" maxlength="100" placeholder="请输入memberId"/>
           </el-form-item>
         </el-col>
         <el-col :span="12">
           <el-form-item label="memberName" prop="memberName">
             <el-input v-model="form.memberName" maxlength="255" placeholder="请输入memberName"/>
           </el-form-item>
         </el-col>
      </el-row>
      <el-row :gutter="10">
         <el-col :span="12">
           <el-form-item label="content" prop="content">
             <el-input v-model="form.content" maxlength="255" placeholder="请输入content"/>
           </el-form-item>
         </el-col>
         <el-col :span="12">
           <el-form-item label="nickname" prop="nickname">
             <el-input v-model="form.nickname" maxlength="255" placeholder="请输入nickname"/>
           </el-form-item>
         </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="phone" prop="phone">
            <el-input type="number" v-model="form.phone" placeholder="请输入phone"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="0" prop="status">
            <el-input type="number" v-model="form.status" placeholder="请输入0"/>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
         <el-col :span="12">
           <el-form-item label="result" prop="result">
             <el-input v-model="form.result" maxlength="255" placeholder="请输入result"/>
           </el-form-item>
         </el-col>
         <el-col :span="12">
           <el-form-item label="updatedTime">
             <el-date-picker v-model="form.updatedTime" type="datetime" placeholder="请选择updatedTime"/>
           </el-form-item>
         </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import {ElMessage, FormInstance, FormRules} from "element-plus";
import {addFeedback, getFeedback, updateFeedback} from "@/api/shop/feedback";

const formRef = ref<FormInstance>()

const emits = defineEmits<{
  (event: 'refresh'): void
}>()

// 表单
let form: any = ref({
    id: null,
    memberId: null,
    memberName: null,
    content: null,
    nickname: null,
    phone: null,
    status: null,
    result: null,
    updatedTime: null,
});

// 效验规则
const rules = reactive<FormRules>({
  content: [
    {required: true, message: '请输入content', trigger: 'blur'},
  ],
})

// 获取详情
const getDetails = (id: string) => {
  getFeedback(id).then(res => {
    form.value = Object.assign({}, form.value, res);
  })
}

// 弹框数据
const dialogData = reactive({
  isShow: false,
  id: null
})

// 打开弹框
const openDialog = async (id: string) => {
  dialogData.isShow = true;
  dialogData.title = '新增';
  if (id) {
    dialogData.id = id;
    dialogData.title = '编辑';
    getDetails(id);
  }
}

// 关闭弹框
const closeDialog = () => {
  dialogData.isShow = false;
  form.value = {};
}
// 提交

const submit = async () => {
   if (!formRef.value) return;
   await formRef.value.validate((valid: any) => {
   if (valid) {
       let data = form.value;
     if (data.id) {
       updateFeedback({...data}).then(() => {
         ElMessage.success('操作成功');
         closeDialog();
         emits('refresh');
       })
     } else {
       addFeedback({...data}).then(() => {
       ElMessage.success('操作成功');
       closeDialog();
       emits('refresh');
       })
     }
   }
  })
}

defineExpose({
  openDialog,
});
</script>

<style scoped>

</style>
